<?php

class Event_Model extends MySQL_Driver {
    public $id;
    public $mail;
    public $motiu;
    public $titol;
    public $categoria;
    public $datainici;
    public $horainici;
    
    public $horafi;
    
    # Mails del usuaris invitats
    public $invitatsevent = array();
    public $invitatseventobjecte = array();
    
    # Constructor d'events
    # També es desa els invitats si en té
    function __construct($id, $mail, $motiu, $titol, $categoria, $datainici, $horainici, $horafi, $stringemails) {
        if($id !== NULL) $this->setId($id);
        $this->setMail($mail);
        $this->setMotiu($motiu);
        $this->setTitol($titol);
        $this->setCategoria($categoria);
        $this->setDataInici($datainici);
        $this->setHoraInici($horainici);      
        $this->setHoraFi($horafi);
        if($stringemails != NULL) $this->setEmailsInvitats($stringemails);
    }
    
    public function delete() {
        $this->query = 
                "
                DELETE  FROM event
                WHERE id = ".$this->getId();
        $this->execute_single_query();
    }
    
    public function __destruct() {
        unset($this);
    }
    
    # Desar en la BBDD el event create
    public function set() {
        $this->query =
                "
                INSERT INTO event(mail, motiu, titol, categoria, datainici, horainici, horafi)
                VALUES('".$this->getMail()."', '".$this->getMotiu()."', '".$this->getTitol()."', '".$this->getCategoria()."', '".$this->getDataInici()."', '".$this->getHoraInici()."', '".$this->getHoraFi()."')
                ";
        $id_a_guardar = $this->execute_single_query();
        $this->setId($id_a_guardar);        
        
        if($this->getCategoria() != "Privat") {
            $this->query = 
                "
                SELECT MAX(id) as id
                FROM event
                "; 

            $this->get_results_from_query();
        
            $id_event = 0;
            foreach ($this->rows as $value) {
                if($value["id"] == '') break;
                $id_event = $value["id"];
                //print $id_event."<br />";
            }

            for($i = 0; $i<count($this->invitatsevent); $i++) {
                $this->query =
                    "
                    INSERT INTO invitats (idevent, mailuser)
                    VALUES(".$id_event.", '".$this->getEmailsInvitat($i)."')
                    ";
                //print $this->query."<br />";
                $this->execute_single_query();

                $this->invitatseventobjecte[$i] = new Invitat_Model($id_event, $this->getEmailsInvitat($i));
            }
        }
        unset($this->rows);        
    }
    
    # Edició de l'usuari contra la BBDD
    function edit($motiu, $titol, $categoria, $datainici, $horainici, $horafi) {
        $this->setMotiu($motiu);
        $this->setTitol($titol);
        $this->setCategoria($categoria);
        $this->setDataInici($datainici);
        $this->setHoraInici($horainici);      
        $this->setHoraFi($horafi);
        
        $this->query = 
                "
                UPDATE event
                SET motiu = '".$this->getMotiu()."' , titol = '".$this->getTitol()."' , categoria = '".$this->getCategoria()."', datainici = '".$this->getDataInici()."', horainici = '".$this->getHoraInici()."', horafi = '".$this->getHoraFi()."'
                WHERE id = ".$this->getId();
            
        $this->execute_single_query();
    }
    
    function getId() {
        return $this->id;
    }
    
    function setId($id) {
        $this->id = $id;
    }
    
    function getMail() {
        return $this->mail;
    }
    
    function setMail($mail) {
        $this->mail = $mail;
    }
    
    function getMotiu() {
        return $this->motiu;
    }
    
    function setMotiu($motiu) {
        $this->motiu = $motiu;
    }
    
    function getTitol() {
        return $this->titol;
    }
    
    function setTitol($titol) {
        $this->titol = $titol;
    }
    
    function getCategoria() {
        return $this->categoria;
    }
    
    function setCategoria($categoria) {
        $this->categoria = $categoria;
    }
    
    function getDataInici() {
        return $this->datainici;
    }
    
    function setDataInici($datainici) {
        $data_original = $datainici;
        $nova_data = date("Y-m-d", strtotime($data_original));
        
        $this->datainici = $nova_data;
    }
    
    function getHoraInici() {
        return $this->horainici;
    }
    
    function setHoraInici($horainici) {
        $this->horainici = $horainici;
    }
    
    function getHoraFi() {
        return $this->horafi;
    }
    
    function setHoraFi($horafi) {
        $this->horafi = $horafi;
    }
    
    function getEmailsInvitat($posicio){
        return $this->invitatsevent[$posicio];
    }
    
    function setEmailsInvitats($stringemails){
       $this->invitatsevent = explode(",", $stringemails);
    }
    
    function getNumberOfInvitats() {
        return count($this->invitatseventobjecte);
    }
}

?>